package com.yk.i_wms.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.model.SmsStockBakeLog;
import com.yk.i_wms.model.SmsStockInfo;
import com.yk.i_wms.service.ISmsStockInfoService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 库存信息
 *
 * @author hbq
 * @date 2021-04-03 14:15:00
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "库存信息")
@RequestMapping("sms")
public class SmsStockInfoController {
    @Autowired
    private ISmsStockInfoService smsStockInfoService;

    /**
     * 拆签
     */
    @ApiOperation(value = "拆签")
    @PostMapping("/smsStockInfo/spilt")
    public Result open(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.spilt(params);
    }
    /**
     * 合签
     */
    @ApiOperation(value = "合签")
    @PostMapping("/smsStockInfo/combine")
    public Result close(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.combine(params);
    }

    /**
     * 通过sn列表获取sn详情
     */
    @ApiOperation(value = "通过sn列表获取sn详情")
    @PostMapping("/smsStockInfo/getSnInfoBySn")
    public Result getSnInfoBySn(@RequestBody Map<String, List<String>> params) {
        List<String> snList = params.get("snList");
        List<SmsStockInfo> stockInfos=new ArrayList<>();
        for (String sn : snList) {
            SmsStockInfo stockInfo = smsStockInfoService.getOne(new LambdaQueryWrapper<SmsStockInfo>().eq(SmsStockInfo::getSn, sn));
            stockInfos.add(stockInfo);
        }
        return Result.succeed(stockInfos,"查询成功");
    }

    /**
     * 查询未打印的拆分标签列表
     */
    @ApiOperation(value = "查询未打印的拆分标签列表")
    @PostMapping("/smsStockInfo/getOpenBarcode")
    public Result getOpenBarcode() {
        List<SmsStockInfo> stockInfos = smsStockInfoService.list(new LambdaQueryWrapper<SmsStockInfo>()
                .eq(SmsStockInfo::getIsOpen, 1)
                .eq(SmsStockInfo::getHasPrint, 0)
        );
        return Result.succeed(stockInfos,"查询成功");
    }

    /**
     * 根据条码修改条码打印状态
     */
    @ApiOperation(value = "根据条码修改条码打印状态")
    @PostMapping("/smsStockInfo/updateBarcodeStatus")
    public Result updateBarcodeStatus(@RequestBody List<SmsStockInfo> smsStockInfos) {
        for (SmsStockInfo stockInfo : smsStockInfos) {
            smsStockInfoService.update(new LambdaUpdateWrapper<SmsStockInfo>()
                    .eq(SmsStockInfo::getSn,stockInfo.getSn())
                    .set(SmsStockInfo::getHasPrint,1)
            );
        }
        return Result.succeed("保存成功");
    }
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/smsStockInfo/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsStockInfoService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 汇总列表
     */
    @ApiOperation(value = "汇总列表")
    @PostMapping("/smsStockInfo/listTotal")
    public Result<PageResult> listTotal(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsStockInfoService.findTotalList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }
    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/smsStockInfo/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        SmsStockInfo model = smsStockInfoService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/smsStockInfo/save")
    public Result save(@RequestBody SmsStockInfo smsStockInfo) {
        smsStockInfoService.saveOrUpdate(smsStockInfo);
        return Result.succeed("保存成功");
    }

    @ApiOperation(value = "延期")
    @PostMapping("/smsStockInfo/delay")
    public Result delay(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.delay(params);
    }

    @ApiOperation(value = "烘烤")
    @PostMapping("/smsStockInfo/bake")
    public Result bake(@RequestBody SmsStockBakeLog smsStockBakeLog) {
        return smsStockInfoService.bake(smsStockBakeLog);
    }

    @ApiOperation(value = "料盘查询")
    @PostMapping("/smsStockInfo/getInfoBySn")
    public Result getInfoBySn(@RequestBody Map<String, Object> params) {
        String sn = MapUtil.getStr(params, "sn");
        SmsStockInfo stockInfo = smsStockInfoService.getOne(new LambdaQueryWrapper<SmsStockInfo>().eq(SmsStockInfo::getSn, sn));
        if (stockInfo == null) {
            return Result.failed("无此条码");
        }
        return Result.succeed(stockInfo,"查询成功");
    }

    @ApiOperation(value = "上架")
    @PostMapping("/smsStockInfo/putAway")
    public Result putAway(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.putAway(params);
    }

    @ApiOperation(value = "下架")
    @PostMapping("/smsStockInfo/soldOut")
    public Result soldOut(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.soldOut(params);
    }

    @ApiOperation(value = "换库")
    @PostMapping("/smsStockInfo/change")
    public Result change(@RequestBody Map<String, Object> params) {
        return smsStockInfoService.change(params);
    }
    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/smsStockInfo/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<SmsStockInfo>> map) {
        List<SmsStockInfo> models = map.get("models");
        smsStockInfoService.saveOrUpdateBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/smsStockInfo/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("ids");
        smsStockInfoService.removeByIds(ids);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/smsStockInfo/leadIn")
    public  Result leadIn(MultipartFile excel) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<SmsStockInfo> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, SmsStockInfo.class);
            rowNum = list.size();
            if (rowNum > 0) {
                list.forEach(u -> {
                        smsStockInfoService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/smsStockInfo/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<SmsStockInfo> smsStockInfoList = ids==null||ids.isEmpty()? smsStockInfoService.list(new QueryWrapper<>()):(List)smsStockInfoService.listByIds(ids);
        //导出操作
        EasyPoiExcelUtil.exportExcel(smsStockInfoList, "库存信息导出", "库存信息导出", SmsStockInfo.class, "smsStockInfo.xls", response);
    }
}
